package lx.exalarm.utils;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import lx.exalarm.app.ThisApp;

/**
 * created by liuxu. many methods here are collected from various site.
 *
 * methods about device info.
 */
public final class DeviceUtils {

    private static Application sApp = ThisApp.getInstance();

    private DeviceUtils() {
    }

    /**
     * get Android OS version, like 4.2.2
     * @return OS version
     */
    public static String getOSVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * get api level:
     *
     * Android 5.0      ->  21  LOLLIPOP
     * Android 4.4W     ->  20  KITKAT_WATCH
     * Android 4.4      ->  19  KITKAT
     * Android 4.3      ->  18  JELLY_BEAN_MR2
     * Android 4.2      ->  17  JELLY_BEAN_MR1
     * Android 4.1      ->  16  JELLY_BEAN
     * Android 4.0.3    ->  15  ICE_CREAM_SANDWICH_MR1
     * Android 4.0      ->  14  ICE_CREAM_SANDWICH
     * Android 3.2      ->  13  HONEYCOMB_MR2
     * Android 3.1      ->  12  HONEYCOMB_MR1
     * Android 3.0      ->  11  HONEYCOMB
     * Android 2.3.3    ->  10  GINGERBREAD_MR1
     * Android 2.3      ->  9   GINGERBREAD
     * Android 2.2      ->  8   FROYO
     * Android 2.1      ->  7   ECLAIR_MR1
     * Android 2.0.1    ->  6   ECLAIR_0_1
     * Android 2.0      ->  5   ECLAIR
     * Android 1.6      ->  4   DONUT
     * Android 1.5      ->  3   CUPCAKE
     * Android 1.1      ->  2   BASE_1_1
     * Android 1.0      ->  1   BASE
     *
     * @return SDK version
     */
    public static int getAndroidSDKVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }

    /**
     * get device model
     * @return android.os.build.MODEL
     */
    public static String getDeviceModel() {
        return android.os.Build.MODEL;
    }

    /**
     * get device brand
     * @return android.os.Build.BRAND
     */
    public static String getTerminalBrand() {
        String brand;
        try {
            brand = android.os.Build.BRAND;
            if (TextUtils.isEmpty(brand)) {
                Class<android.os.Build> build_class = android.os.Build.class;
                java.lang.reflect.Field field;
                field = build_class.getField("BRAND");
                brand = (String) field.get(new android.os.Build());
            }
        } catch (Exception e) {
            e.printStackTrace();
            brand = "";
        }
        return brand;
    }

    /**
     * returns the unique device ID:
     * IMEI for GSM, MEID or ESN for CDMA.
     * return null if device ID is not available.
     * Requires Permission: READ_PHONE_STATE
     */
    public static String getIMEI(){
        android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager)
                sApp.getSystemService(Context.TELEPHONY_SERVICE);
        // Requires Permission: READ_PHONE_STATE
        String device_id = tm.getDeviceId();
        if (TextUtils.isEmpty(device_id)) {
            device_id = android.provider.Settings.Secure.getString(
                    sApp.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
        }
        return device_id;
    }

    /**
     * returns a unique id for the device:
     * return IMEI if available. if not, return ANDROID_ID from settings provider
     * @return a unique id
     */
    public static String getDeviceId() {
        String id = getIMEI();
        if (TextUtils.isEmpty(id)) {
            id = android.provider.Settings.Secure.getString(
                    sApp.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
        }
        return id;
    }

    /**
     * get mac address
     */
    public static String getMacAddress() {
        // TODO
        android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager)
                sApp.getSystemService(Context.WIFI_SERVICE);
        String mac = wifi.getConnectionInfo().getMacAddress();
        if(mac == null){
            mac = "";
        }
        return  mac;
    }

    // TODO
    public static String getIpAddress() {
        return null;
    }

}
